package com.hualan.sfzy.controller.ssh;

import com.hualan.sfzy.model.entity.Answer;
import com.hualan.sfzy.model.entity.PsychologyTest;
import com.hualan.sfzy.model.vo.AnswerCount;
import com.hualan.sfzy.service.impl.AnswerServiceImpl;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/answer")
public class AnswerController {
    @Autowired
    private AnswerServiceImpl Service;

    /*添加*/
    @PostMapping("/add")
    public Result add(@RequestBody Answer entity) {
        int i = Service.add(entity);
        if (i > 0) {
            return Result.success(i);
        }
        return Result.error();
    }

    /*编辑*/
    @PutMapping("/edit")
    public Result edit(@RequestBody Answer entity) {
        int i = Service.edit(entity);
        if (i > 0) {
            return Result.success();
        }
        return Result.error();
    }

    /*删除*/
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") Integer id) {
        int i = Service.remove(id);
        if (i > 0) {
            return Result.success(i);
        }
        return Result.error();
    }

    /*批量删除*/
    @DeleteMapping("/delBatch")
    public Result delBatch(Integer[] ids) {
        // 参数校验
        if (ids == null || ids.length == 0) {
            return Result.error("请提供有效的删除 ID 列表");
        }
        int i = Service.removeBatch(ids);
        if (i > 0) {
            return Result.success(i);
        }
        return Result.error("未删除任何记录");

    }


    /*查询所有*/
    @GetMapping("/find")
    public Result find() {
        List<Answer> list = Service.find();
        if (list.size() > 0) {
            return Result.success(list);
        }
        return Result.error();
    }

    /*分页查询所有*/
    @GetMapping("/findPage")
    public Result findPage(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                           Answer entity) {
        Map<String, Object> map = Service.findPage(pageNum, pageSize, entity);
        if (map != null && !map.isEmpty()) {
            return Result.success(map);
        }
        return Result.error();
    }

    /*根据id查询单个*/
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        if (id == null || id <= 0) {
            return Result.error("ID参数不合法");
        }
        Answer answer = Service.findById(id);
        if (answer != null) {
            return Result.success(answer);
        }
        return Result.error("未找到ID为" + id + "的记录");
    }

    /*根据id查询单个(多表)*/
    @GetMapping("/findByIdDuo/{id}")
    public Result findByIdDuo(@PathVariable("id") Integer id) {
        if (id == null || id <= 0) {
            return Result.error("ID参数不合法");
        }
        Answer answer = Service.selectByIdDuo(id);
        if (answer != null) {
            return Result.success(answer);
        }
        return Result.error("未找到ID为" + id + "的多表记录");
    }


    /*查询全部(多表) 带条件*/
    @GetMapping("/selectDuo")
    public Result selectDuo(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                            @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                            Answer entity) {
        Map<String, Object> map = Service.selectDuo(pageNum, pageSize, entity);
        if (map != null && !map.isEmpty()) {
            return Result.success(map);
        }
        return Result.error();
    }
    @GetMapping("/userid")
    public Result userid(@RequestParam Integer userid) {

        return Result.success(Service.selectUserid(userid));

    }



}
